import {
    defineStore
} from "pinia";
import {
    ref
} from "vue";

export const useUserStore = defineStore('user', () => {
    const user = ref({
        id: null,
        username: null,
        email: null,
        role: null,
        token: null
    })

    //存用户信息
    function setUser(userData) {
        user.value = userData
    }

    //删用户信息
    function clearUser() {
        user.value = {
            id: null,
            username: null,
            email: null,
            role: null,
            token: null
        }
        //清除本地存储
        localStorage.removeItem('user-store')
    }

    return {
        user,
        setUser,
        clearUser
    }
},{
    persist: {
        keys: 'user-store',
        storage: localStorage,
        paths: ['user']
    }
})